package hu.bme.aut.consulttime.client.components;

import java.util.List;

import hu.bme.aut.consulttime.client.StudentTeamMembershipService;
import hu.bme.aut.consulttime.client.StudentTeamMembershipServiceAsync;
import hu.bme.aut.consulttime.entitydtos.StudentTeamMembershipDTO;
import hu.bme.aut.consulttime.entitydtos.UserDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.TabPanel;

public class StudentRootPanel extends Composite{

	private ListBox projectTeamsListBox;
	private TabPanel tabPanel;
	private TeamInfoPage teamInfoPage;
	private ProjectInfoPage projectInfoPage;
	private StudentToDoPage studentToDoPage;

	
	private StudentTeamMembershipServiceAsync studentTeamMembershipService = GWT.create(StudentTeamMembershipService.class);
	
	private UserDTO userData = null;
	private List<StudentTeamMembershipDTO> studentTeamMemberships = null;

	public StudentRootPanel(UserDTO userData) {
		
		this.userData = userData;
		
		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSpacing(10);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		verticalPanel.setCellVerticalAlignment(horizontalPanel, HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel.setCellHorizontalAlignment(horizontalPanel, HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel.setSize("100%", "30px");
		horizontalPanel.setSpacing(5);
		
		Label lblNewLabel = new Label("Project teams:");
		lblNewLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel.add(lblNewLabel);
		lblNewLabel.setWidth("96px");
		horizontalPanel.setCellVerticalAlignment(lblNewLabel, HasVerticalAlignment.ALIGN_MIDDLE);
		
		projectTeamsListBox = new ListBox();
		horizontalPanel.add(projectTeamsListBox);
		horizontalPanel.setCellWidth(projectTeamsListBox, "100%");
		projectTeamsListBox.setWidth("100%");
		horizontalPanel.setCellVerticalAlignment(projectTeamsListBox, HasVerticalAlignment.ALIGN_MIDDLE);
		projectTeamsListBox.addItem("Select a project team in which you participate...");
		
		tabPanel = new TabPanel();
		verticalPanel.add(tabPanel);
		verticalPanel.setCellHorizontalAlignment(tabPanel, HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setCellVerticalAlignment(tabPanel, HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel.setCellHeight(tabPanel, "100%");
		verticalPanel.setCellWidth(tabPanel, "100%");
		tabPanel.setSize("100%", "100%");
		tabPanel.setVisible(false);
		tabPanel.setAnimationEnabled(true);
		
		teamInfoPage = new TeamInfoPage();
		tabPanel.add(teamInfoPage, "Team", false);
		teamInfoPage.setSize("100%", "100%");
		
		projectInfoPage = new ProjectInfoPage();
		tabPanel.add(projectInfoPage, "Project", false);
		projectInfoPage.setSize("100%", "100%");
		
		studentToDoPage = new StudentToDoPage();
		tabPanel.add(studentToDoPage, "To-dos", false);
		studentToDoPage.setSize("100%", "100%");
		studentToDoPage.setUserId(userData.getUserId());
		
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() 
		        {
		            public void onSelection(SelectionEvent<Integer> event) 
		            {
		                int tabId = event.getSelectedItem();
		                switch (tabId) {
						case 0:
							teamInfoPage.loadTeam();
							teamInfoPage.loadTeamMembers();
							break;
						case 1:
							projectInfoPage.loadProject();
							projectInfoPage.loadProjectTutors();
							break;
						case 2:
							studentToDoPage.loadTeamMembers();
							studentToDoPage.loadProjectTutors();
							break;
						default:
							break;
						}
		            }
		        });		
		
		projectTeamsListBox.addChangeHandler(new ChangeHandler() {
			
			@Override
			public void onChange(ChangeEvent event) {
				 int selectedIndex = projectTeamsListBox.getSelectedIndex();
				 if (selectedIndex != 0) {

					 if (studentTeamMemberships != null) {
						 tabPanel.setVisible(true);
						 teamInfoPage.setTeamId(studentTeamMemberships.get(selectedIndex-1).getTeamId());
						 projectInfoPage.setProjectId(studentTeamMemberships.get(selectedIndex-1).getProjectId());
						 studentToDoPage.setTeamId(studentTeamMemberships.get(selectedIndex-1).getTeamId());
						 studentToDoPage.setProjectId(studentTeamMemberships.get(selectedIndex-1).getProjectId());
						 tabPanel.selectTab(0);
					 }
				 }
				 else {
					 tabPanel.setVisible(false);
				 }
			}
		});
		
		loadStudentTeamMemberships();
	}
	
	public void loadStudentTeamMemberships() {
		studentTeamMembershipService.retrieveStudentTeamMemberships(userData.getUserId(), new AsyncCallback<List<StudentTeamMembershipDTO>>() {
			
			@Override
			public void onSuccess(List<StudentTeamMembershipDTO> result) {
				studentTeamMemberships = result;
				
				for (StudentTeamMembershipDTO stm : result) {
					String title = stm.getSemesterId() + " " + stm.getProjectType().toString() + " - " + stm.getProjectTitle();
					projectTeamsListBox.addItem(title, String.valueOf(stm.getTeamId()));
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Remote Procedure Call - Failure");
			}
		});
	}

}
